﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using clf.weighing.entity;
using HandyControl.Controls;


namespace weighing
{
    /// <summary>
    /// BillEditWimpleWindow.xaml 简易过磅模式的修改 的交互逻辑
    /// </summary>
    public partial class BillEditSimpleWindow : GlowWindow
    {
        #region  variables

        private WeighingBill? mWeighingBill;
        private bool isFristBindValue = false;
        private bool isSaving = false;
        #endregion

        public BillEditSimpleWindow(WeighingBillVo? billVo)
        {
            InitializeComponent();
            CommonFunction.ShowCustomIcon(this);

            if (billVo == null)
            {
                CommonFunction.AlertInfo("传入的参数不正确，窗口将关闭。");
                this.Close();
            }

            mWeighingBill = billVo?.mWeighingBill;
        }

        #region  win event
        private void Bill_Edit_Window_Loaded(object sender, RoutedEventArgs e)
        {
            //动态插入备用字段，并保存修改后的值
            CreateBackFillUI();

            SetDefaultSource();

            if (App.CurrentUser.roleType < (int)UserRoleType.WeighingManager)
            {
                this.grossWeightTb.IsEnabled = false;
                this.TraeWeightTb.IsEnabled = false;
                this.netWeightTb.IsEnabled = false;
            }
            else
            {
                this.grossWeightTb.IsEnabled = true;
                this.TraeWeightTb.IsEnabled = true;
                this.netWeightTb.IsEnabled = true;
            }
        }

        private void Bill_Edit_Window_ContentRendered(object sender, EventArgs e)
        {
            BindValue();
            SetBackFieldValue();
        }

        private void Bill_Edit_Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Escape:
                    this.Close();
                    break;
            }
        }

        private void Bill_Edit_Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }
        #endregion


        private void BindValue()
        {
            isFristBindValue = true;
            this.BillInfoTb.Text = "编号：" + mWeighingBill?.number;
            this.billnumberTb.Text = mWeighingBill?.number;
            this.sendCompanyCb.Text = mWeighingBill?.sendCompanyName?.ToString() ?? "";
            this.RecevieCompanyCb.Text = mWeighingBill?.receiveCompanyName?.ToString() ?? "";
            this.materialCb.Text = mWeighingBill?.materialName?.ToString() ?? "";
            this.grossWeightTb.Text = mWeighingBill?.grossWeight.ToString();
            this.TraeWeightTb.Text = mWeighingBill?.traeWeight.ToString();
            this.netWeightTb.Text = mWeighingBill?.netWeight.ToString();
            this.remarkCb.Text = mWeighingBill?.remark?.ToString() ?? "";
            this.userTb.Text = mWeighingBill?.inUserName?.ToString() ?? "";
            this.carNumberCb.Text = mWeighingBill?.carNumber?.ToString() ?? "";
            this.driverTb.Text = mWeighingBill?.driver?.ToString() ?? "";
            this.phoneTb.Text = mWeighingBill?.driverMobile?.ToString() ?? "";
            this.inDateTime.Text = mWeighingBill?.inTime?.ToString(Constant.DateTimeFormat) ?? "";
            this.outDateTime.Text = mWeighingBill?.outTime?.ToString(Constant.DateTimeFormat) ?? "";

            this.TempPriceTb.Text = mWeighingBill?.price.ToString() ?? "0";
            this.TotalMoneyTb.Text = mWeighingBill?.totalMoney?.ToString() ?? "0";
            this.ReceiveMoneyTb.Text = mWeighingBill?.receiveMoney.ToString() ?? "0";
            this.MoneyTypeCb.SelectedIndex = (mWeighingBill?.moneyType)??0;
            isFristBindValue = false;
        }

        #region 车牌号输入

        private void KeyBoardBtn_Click(object sender, RoutedEventArgs e)
        {
            this.carInputPopup.IsOpen = !this.carInputPopup.IsOpen;
            if (this.carInputPopup.IsOpen)
            {
                this.carNumberInput.Complated += On_Complated;
            }
            else
            {
                this.carNumberInput.Complated -= On_Complated;
            }
        }

        private void On_Complated(string res)
        {
            this.carNumberCb.Text = res;
            this.carInputPopup.IsOpen = false;
        }
        #endregion


        #region backup filed

        /// <summary>
        /// 修改创建 备用字段的UI
        /// </summary>
        private void CreateBackFillUI()
        {
            List<BackupBillField> backupBills = BackupBillFieldDao.Instance.SelectAllByType(mWeighingBill.type);
            if (backupBills == null || backupBills.Count == 0)
            {
                this.backFieldPanel.Children.Clear();
            }
            else
            {
                this.backFieldPanel.Children.Clear();
                for (int i = 0; i < backupBills.Count; i++)
                {
                    var item = backupBills[i];
                    int w = item.width < 200 ? 200 : item.width;
                    w = item.width > 400 ? 400 : item.width;
                    if (item.isEnable == 1)
                    {
                        if (item.controlType == (int)BackFieldControlType.Combox)
                        {
                            var combobox = new System.Windows.Controls.ComboBox()
                            {
                                Name = item.fiedName + "CB",
                                Style = FindResource("ComboBoxExtend") as Style,
                                Margin = new Thickness(0, 2, 0, 2),
                                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                                MinWidth = 100,
                                MaxWidth = 400,
                                IsEditable = true,
                                Width = w,
                                FontSize = 14
                            };
                            InfoElement.SetTitle(combobox, item.displayName);
                            InfoElement.SetTitlePlacement(combobox, HandyControl.Data.TitlePlacementType.Left);
                            InfoElement.SetTitleWidth(combobox, new GridLength(Convert.ToDouble(item.titleWidth)));

                            this.backFieldPanel.Children.Add(combobox);
                        }
                        else
                        {
                            var textBox = new System.Windows.Controls.TextBox()
                            {
                                Name = item.fiedName + "TB",
                                Style = FindResource("TextBoxExtend") as Style,
                                Margin = new Thickness(0, 2, 0, 2),
                                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                                MinWidth = 100,
                                MaxWidth = 400,
                                Width = w,
                                FontSize = 14
                            };
                            InfoElement.SetTitle(textBox, item.displayName);
                            InfoElement.SetTitlePlacement(textBox, HandyControl.Data.TitlePlacementType.Left);
                            InfoElement.SetTitleWidth(textBox, new GridLength(Convert.ToDouble(item.titleWidth)));

                            this.backFieldPanel.Children.Add(textBox);
                        }
                    }
                }
            }
        }


        /// <summary>
        /// 设置备用字段的值
        /// </summary>
        private bool SetBackFieldValue()
        {
            if (this.backFieldPanel.Children.Count == 0) return true;
            for (int i = 0; i < this.backFieldPanel.Children.Count; i++)
            {
                try
                {
                    var child = this.backFieldPanel.Children[i] as System.Windows.Controls.TextBox;
                    if (child != null)
                    {
                        var name = child.Name.Replace("TB", "");
                        if (name == Constant.BackfieldName.BackString1)
                        {
                            child.Text = mWeighingBill.backupString1;
                        }
                        else if (name == Constant.BackfieldName.BackString2)
                        {
                            child.Text = mWeighingBill.backupString2;
                        }
                        else if (name == Constant.BackfieldName.BackString3)
                        {
                            child.Text = mWeighingBill.backupString3;
                        }
                        else if (name == Constant.BackfieldName.BackString4)
                        {
                            child.Text = mWeighingBill.backupString4;
                        }
                        else if (name == Constant.BackfieldName.BackString5)
                        {
                            child.Text = mWeighingBill.backupString5;
                        }
                        else if (name == Constant.BackfieldName.BackInt1)
                        {
                            child.Text = mWeighingBill.backupInt1.ToString();
                        }
                        else if (name == Constant.BackfieldName.BackInt2)
                        {
                            child.Text = mWeighingBill.backupInt2.ToString();
                        }
                        else if (name == Constant.BackfieldName.BackInt3)
                        {
                            child.Text = mWeighingBill.backupInt3.ToString();
                        }
                        else if (name == Constant.BackfieldName.BackInt4)
                        {
                            child.Text = mWeighingBill.backupInt4.ToString();
                        }
                        else if (name == Constant.BackfieldName.BackInt5)
                        {
                            child.Text = mWeighingBill.backupInt5.ToString();
                        }
                        else if (name == Constant.BackfieldName.BackDecimal1)
                        {
                            child.Text = mWeighingBill.backupDecimal1.ToString();
                        }
                        else if (name == Constant.BackfieldName.BackDecimal2)
                        {
                            child.Text = mWeighingBill.backupDecimal2.ToString();
                        }
                        else if (name == Constant.BackfieldName.BackDecimal3)
                        {
                            child.Text = mWeighingBill.backupDecimal3.ToString();
                        }
                        else if (name == Constant.BackfieldName.BackDecimal4)
                        {
                            child.Text = mWeighingBill.backupDecimal4.ToString();
                        }
                        else if (name == Constant.BackfieldName.BackDecimal5)
                        {
                            child.Text = mWeighingBill.backupDecimal5.ToString();
                        }
                    }
                    else
                    {
                        //child is null means control is  ComboBox
                        var cb = this.backFieldPanel.Children[i] as System.Windows.Controls.ComboBox;
                        if (cb != null)
                        {
                            var name = cb.Name.Replace("CB", "");
                            if (name == Constant.BackfieldName.BackString1)
                            {
                                cb.Text = mWeighingBill.backupString1;
                            }
                            else if (name == Constant.BackfieldName.BackString2)
                            {
                                cb.Text = mWeighingBill.backupString2;
                            }
                            else if (name == Constant.BackfieldName.BackString3)
                            {
                                cb.Text = mWeighingBill.backupString3;
                            }
                            else if (name == Constant.BackfieldName.BackString4)
                            {
                                cb.Text = mWeighingBill.backupString4;
                            }
                            else if (name == Constant.BackfieldName.BackString5)
                            {
                                cb.Text = mWeighingBill.backupString5;
                            }
                            else if (name == Constant.BackfieldName.BackInt1)
                            {
                                cb.Text = mWeighingBill.backupInt1.ToString();
                            }
                            else if (name == Constant.BackfieldName.BackInt2)
                            {
                                cb.Text = mWeighingBill.backupInt2.ToString();
                            }
                            else if (name == Constant.BackfieldName.BackInt3)
                            {
                                cb.Text = mWeighingBill.backupInt3.ToString();
                            }
                            else if (name == Constant.BackfieldName.BackInt4)
                            {
                                cb.Text = mWeighingBill.backupInt4.ToString();
                            }
                            else if (name == Constant.BackfieldName.BackInt5)
                            {
                                cb.Text = mWeighingBill.backupInt5.ToString();
                            }
                            else if (name == Constant.BackfieldName.BackDecimal1)
                            {
                                cb.Text = mWeighingBill.backupDecimal1.ToString();
                            }
                            else if (name == Constant.BackfieldName.BackDecimal2)
                            {
                                cb.Text = mWeighingBill.backupDecimal2.ToString();
                            }
                            else if (name == Constant.BackfieldName.BackDecimal3)
                            {
                                cb.Text = mWeighingBill.backupDecimal3.ToString();
                            }
                            else if (name == Constant.BackfieldName.BackDecimal4)
                            {
                                cb.Text = mWeighingBill.backupDecimal4.ToString();
                            }
                            else if (name == Constant.BackfieldName.BackDecimal5)
                            {
                                cb.Text = mWeighingBill.backupDecimal5.ToString();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Growl.Error("输入有错：" + ex.Message);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 获取备用字段的值
        /// </summary>
        private bool GetBackFieldValue()
        {
            if (this.backFieldPanel.Children.Count == 0) return true;
            for (int i = 0; i < this.backFieldPanel.Children.Count; i++)
            {
                try
                {
                    var child = this.backFieldPanel.Children[i] as System.Windows.Controls.TextBox;
                    if (child != null)
                    {
                        var name = child.Name.Replace("TB", "");
                        if (name == Constant.BackfieldName.BackString1)
                        {
                            mWeighingBill.backupString1 = child.Text?.Trim();
                        }
                        else if (name == Constant.BackfieldName.BackString2)
                        {
                            mWeighingBill.backupString2 = child.Text?.Trim();
                        }
                        else if (name == Constant.BackfieldName.BackString3)
                        {
                            mWeighingBill.backupString3 = child.Text?.Trim();
                        }
                        else if (name == Constant.BackfieldName.BackString4)
                        {
                            mWeighingBill.backupString4 = child.Text?.Trim();
                        }
                        else if (name == Constant.BackfieldName.BackString5)
                        {
                            mWeighingBill.backupString5 = child.Text?.Trim();
                        }
                        else if (name == Constant.BackfieldName.BackInt1)
                        {

                            mWeighingBill.backupInt1 = Convert.ToInt32(child.Text.Trim());
                        }
                        else if (name == Constant.BackfieldName.BackInt2)
                        {

                            mWeighingBill.backupInt2 = Convert.ToInt32(child.Text.Trim());
                        }
                        else if (name == Constant.BackfieldName.BackInt3)
                        {
                            mWeighingBill.backupInt3 = Convert.ToInt32(child.Text.Trim());
                        }
                        else if (name == Constant.BackfieldName.BackInt4)
                        {

                            mWeighingBill.backupInt4 = Convert.ToInt32(child.Text.Trim());
                        }
                        else if (name == Constant.BackfieldName.BackInt5)
                        {
                            mWeighingBill.backupInt5 = Convert.ToInt32(child.Text.Trim());
                        }
                        else if (name == Constant.BackfieldName.BackDecimal1)
                        {
                            mWeighingBill.backupDecimal1 = Convert.ToDecimal(child.Text.Trim());
                        }
                        else if (name == Constant.BackfieldName.BackDecimal2)
                        {
                            mWeighingBill.backupDecimal2 = Convert.ToDecimal(child.Text.Trim());
                        }
                        else if (name == Constant.BackfieldName.BackDecimal3)
                        {
                            mWeighingBill.backupDecimal3 = Convert.ToDecimal(child.Text.Trim());
                        }
                        else if (name == Constant.BackfieldName.BackDecimal4)
                        {
                            mWeighingBill.backupDecimal4 = Convert.ToDecimal(child.Text.Trim());
                        }
                        else if (name == Constant.BackfieldName.BackDecimal5)
                        {
                            mWeighingBill.backupDecimal5 = Convert.ToDecimal(child.Text.Trim());
                        }
                    }
                    else
                    {
                        var cb = this.backFieldPanel.Children[i] as System.Windows.Controls.ComboBox;
                        if (cb != null)
                        {
                            var name = cb.Name.Replace("CB", "");
                            string value = cb.Text?.Trim();
                            if (name == Constant.BackfieldName.BackString1)
                            {
                                mWeighingBill.backupString1 = value;
                            }
                            else if (name == Constant.BackfieldName.BackString2)
                            {
                                mWeighingBill.backupString2 = value;
                            }
                            else if (name == Constant.BackfieldName.BackString3)
                            {
                                mWeighingBill.backupString3 = value;
                            }
                            else if (name == Constant.BackfieldName.BackString4)
                            {
                                mWeighingBill.backupString4 = value;
                            }
                            else if (name == Constant.BackfieldName.BackString5)
                            {
                                mWeighingBill.backupString5 = value;
                            }
                            else if (name == Constant.BackfieldName.BackInt1)
                            {

                                mWeighingBill.backupInt1 = Convert.ToInt32(value);
                            }
                            else if (name == Constant.BackfieldName.BackInt2)
                            {

                                mWeighingBill.backupInt2 = Convert.ToInt32(value);
                            }
                            else if (name == Constant.BackfieldName.BackInt3)
                            {
                                mWeighingBill.backupInt3 = Convert.ToInt32(value);
                            }
                            else if (name == Constant.BackfieldName.BackInt4)
                            {

                                mWeighingBill.backupInt4 = Convert.ToInt32(value);
                            }
                            else if (name == Constant.BackfieldName.BackInt5)
                            {
                                mWeighingBill.backupInt5 = Convert.ToInt32(value);
                            }
                            else if (name == Constant.BackfieldName.BackDecimal1)
                            {
                                mWeighingBill.backupDecimal1 = Convert.ToDecimal(value);
                            }
                            else if (name == Constant.BackfieldName.BackDecimal2)
                            {
                                mWeighingBill.backupDecimal2 = Convert.ToDecimal(value);
                            }
                            else if (name == Constant.BackfieldName.BackDecimal3)
                            {
                                mWeighingBill.backupDecimal3 = Convert.ToDecimal(value);
                            }
                            else if (name == Constant.BackfieldName.BackDecimal4)
                            {
                                mWeighingBill.backupDecimal4 = Convert.ToDecimal(value);
                            }
                            else if (name == Constant.BackfieldName.BackDecimal5)
                            {
                                mWeighingBill.backupDecimal5 = Convert.ToDecimal(value);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Growl.Error("输入有错：" + ex.Message, "edit");
                    return false;
                }
            }
            return true;
        }

        #endregion

        #region Supply         
        private void SupplyCb_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
        private void SupplyCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sendCompanyCb.SelectedIndex == -1)
            {
                this.sendCompanyCb.Text = "";
                return;
            }

        }

        #endregion

        #region Receiver Company

        private void ReceiverCompanyCb_TextChanged(object sender, TextChangedEventArgs e)
        {
        }
        private void ReceiverCompanyCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (RecevieCompanyCb.SelectedIndex == -1)
            {
                RecevieCompanyCb.Text = "";
                return;
            };
        }

        #endregion

        #region Material 

        private void MaterialNameCb_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void MaterialNameCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (materialCb.SelectedIndex > -1)
            {
                //SetMaterialPrice();

            }

        }
        #endregion

        #region CarInfo Selection       
        private void CarNumberCb_TextChanged(object sender, TextChangedEventArgs e)
        {
            String text = this.carNumberCb.Text.Trim();

        }

        private void CarNumberCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!IsLoaded) return;

            if (carNumberCb.SelectedIndex >= 0)
            {
                CarInfoVo currCarVo = carNumberCb.SelectedItem as CarInfoVo;
                SetCarInfo(currCarVo);

            }
            else
            {

            }
        }

        private void SetCarInfo(CarInfoVo currCarVo)
        {
            if (currCarVo != null)
            {
                this.driverTb.Text = currCarVo.Driver;
                this.phoneTb.Text = currCarVo.DriverMobile;

            }
            else
            {
                this.driverTb.Text = string.Empty;
                this.phoneTb.Text = string.Empty;
            }
        }

        #endregion


        private void GrossWeightTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            CalcReceiptWeightValue();
        }

        private void TraeWeightTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            CalcReceiptWeightValue();
        }

        /// <summary>
        /// 计算收货吨位  calculating the tonnage of receipt
        /// </summary>
        private void CalcReceiptWeightValue()
        {
            if (this.IsLoaded == false || this.IsVisible == false) return;

            decimal gross = 0;
            decimal trae = 0;
            decimal decuation = 0;
            decimal net = 0;                 
            //decimal sendnet = 0;
            //decimal diff;
            try { gross = Convert.ToDecimal(this.grossWeightTb.Text); } catch { }
            try { trae = Convert.ToDecimal(this.TraeWeightTb.Text); } catch { }
           
            if (trae > 0)
            {
                net = gross - trae - decuation;
                this.netWeightTb.Text = net.ToString();
            }
            else
            {
                this.netWeightTb.Text = "0";
            }

            //diff = Math.Round(net - sendnet, 2);
           
        }

        #region save

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            if (isSaving) return;
            isSaving = true;

            GetWeightValue();

            mWeighingBill.sendCompanyId = 0;
            mWeighingBill.sendCompanyName = this.sendCompanyCb.Text;
            mWeighingBill.receiveCompanyId = 0;
            mWeighingBill.receiveCompanyName = this.RecevieCompanyCb.Text;

            mWeighingBill.materialId = 0;
            mWeighingBill.materialName = this.materialCb.Text;

            mWeighingBill.carId = 0;
            mWeighingBill.carNumber = this.carNumberCb.Text;
            mWeighingBill.driver = this.driverTb.Text;
            mWeighingBill.driverMobile = this.phoneTb.Text;
            mWeighingBill.remark = this.remarkCb.Text;

            if (mWeighingBill.number.IsNullOrEmpty())
            {
                mWeighingBill.number = this.billnumberTb.Text;
            }

            if (mWeighingBill.number.IsNullOrEmpty())
            {
                var msg = "系统出错，请重启称重系统，再试。";
                Growl.Clear();
                Growl.Error(msg, "main");
                this.saveBtn.IsChecked = false;
                isSaving = false;
                return;
            }

            if (mWeighingBill.grossWeight <= 0 && mWeighingBill.traeWeight <= 0 && mWeighingBill.netWeight <= 0)
            {
                var msg = "未读取重量";
                Growl.Clear();
                Growl.Error(msg, "main");
                this.saveBtn.IsChecked = false;
                isSaving = false;
                return;
            }


            try
            {
                DateTime dt;
                DateTime.TryParse(this.inDateTime.Text, out dt);
                mWeighingBill.inTime = dt;
            }
            catch (Exception)
            {
                var msg = "时间格式不正确";
                Growl.Clear();
                Growl.Error(msg, "main");
                this.inDateTime.Focus();
                this.saveBtn.IsChecked = false;
                isSaving = false;
                return;
            }
            try
            {
                DateTime dt;
                DateTime.TryParse(this.outDateTime.Text, out dt);
                mWeighingBill.outTime = dt;
            }
            catch (Exception)
            {
                var msg = "时间格式不正确";
                Growl.Clear();
                Growl.Error(msg, "main");
                this.outDateTime.Focus();
                this.saveBtn.IsChecked = false;
                isSaving = false;
                return;
            }

            GetBackFieldValue();

            GetPriceValue();

            //修改后，需要重先上传 
            mWeighingBill.upOfficalStatus--;
            if (mWeighingBill.upOfficalStatus <= 0) mWeighingBill.upOfficalStatus = 0;

            var res = WeighingBillDao.Instance.Update(mWeighingBill);

            var info = "";
            if (res > 0)
            {
                info = "保存成功";
                Growl.Success(info, "edit");
                DelegeteFactory.Instance.onWeighingDataChanged?.Invoke(WeightingBillType.Mixture);
            }
            else
            {
                info = "保存失败";
                Growl.Error(info, "edit");
            }
            this.saveBtn.IsChecked = false;
            isSaving = false;
        }

        /// <summary>
        /// 获取稳重的数据
        /// </summary>
        private void GetWeightValue()
        {
            if (this.IsLoaded == false || this.IsVisible == false) return;

            decimal gross = 0;
            decimal trae = 0;
            decimal decuation = 0;
            decimal net = 0;
            decimal sendnet = 0;
            try { gross = Convert.ToDecimal(this.grossWeightTb.Text); } catch { }
            //try { sendnet = Convert.ToDecimal(this.sendNetWeightTb.Text); } catch { }
            decimal diff;
            try { trae = Convert.ToDecimal(this.TraeWeightTb.Text); } catch { }
            //try { decuation = Convert.ToDecimal(this.decuationWeightTb.Text); } catch { }
            try { net = Convert.ToDecimal(this.netWeightTb.Text); } catch { }

            diff = Math.Round(net - sendnet, 2);

            mWeighingBill.grossWeight = gross;
            mWeighingBill.traeWeight = trae;
            mWeighingBill.decuationWeight = decuation;
            mWeighingBill.netWeight = net;
            mWeighingBill.diffWeight = diff;
        }
        #endregion

        #region 将本机使用的基础数据设置默认数据源 最新使用的排在最前面
        /// <summary>
        /// 将本机使用的基础数据设置默认数据源 
        /// </summary>
        private void SetDefaultSource()
        {
            var companyS = CompanyDao.Instance.GetList().Select(i => { return new CompanyVo(i); }).ToList();
            var ms = MaterialDao.Instance.GetList().Select(i => { return new MaterialVo(i); }).ToList();
            var cars = CarDao.Instance.GetList().Select(i => { return new CarInfoVo(i); }).ToList();
            this.sendCompanyCb.ItemsSource = companyS;
            this.RecevieCompanyCb.ItemsSource = companyS;
            this.materialCb.ItemsSource = ms;
            this.carNumberCb.ItemsSource = cars;

        }

        #endregion

        #region price calc
        private void NetWeightTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            CalcMaterialMoney();
        }
        private void TempPriceTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            CalcMaterialMoney();
        }
        /// <summary>
        /// 计算货物的金额
        /// </summary>
        private void CalcMaterialMoney()
        {
            decimal price = 0;
            decimal netWeight = 0;
            decimal totalMoney = 0;
            //不管磅单是显示或者 不显示 都要计算 
            if (this.TempPriceTb.Visibility == Visibility.Visible)
            {
                try
                {
                    price = Convert.ToDecimal(this.TempPriceTb.Text.Trim());
                }
                catch (Exception ex)
                {
                    Growl.Error("输入的单价不正确：" + ex.Message, "main");
                }
            }

            if (!this.netWeightTb.Text.IsNullOrEmpty())
            {
                try
                {
                    netWeight = Convert.ToDecimal(this.netWeightTb.Text.Trim());
                }
                catch (Exception)
                {
                }
            }


            if (this.TotalMoneyTb.Visibility == Visibility.Visible)
            {
                totalMoney = (decimal)Math.Round((double)(price * netWeight), 3, MidpointRounding.AwayFromZero);

                this.TotalMoneyTb.Text = totalMoney + "";
            }

            if (this.ReceiveMoneyTb.Visibility == Visibility.Visible)
            {
                string receiveMoneyStr = this.ReceiveMoneyTb.Text.Trim();
                if (receiveMoneyStr.IsNullOrEmpty() || receiveMoneyStr.Equals("0")
                    || receiveMoneyStr.Equals("0.0")
                    || receiveMoneyStr.Equals("0.00")
                    || receiveMoneyStr.Equals("0.000"))
                {
                    this.ReceiveMoneyTb.Text = totalMoney + "";
                }
            }

        }


        private void GetPriceValue()
        {
            //不管磅单是显示或者 不显示 都要计算 
            decimal price = 0;
            decimal receiveMoney = 0;
            if (!this.TempPriceTb.Text.IsNullOrEmpty())
            {
                try
                {
                    price = Convert.ToDecimal(this.TempPriceTb.Text.Trim());
                }
                catch (Exception ex)
                {
                    Console.WriteLine("输入的单价不正确：" + ex.Message, "main");
                }
            }

            var totalMoney = (decimal)Math.Round((double)(price * mWeighingBill?.netWeight ?? 0), 3, MidpointRounding.AwayFromZero);
            mWeighingBill.totalMoney = totalMoney;
            mWeighingBill.price = price;
            mWeighingBill.priceId = 0;
            string receiveMoneyStr = this.ReceiveMoneyTb.Text.Trim();
            if (!receiveMoneyStr.IsNullOrEmpty())
            {
                try
                {
                    receiveMoney = Convert.ToDecimal(receiveMoneyStr);
                }
                catch (Exception ex)
                {

                }
                mWeighingBill.receiveMoney = receiveMoney;
            }

            if (this.MoneyTypeCb.Visibility == Visibility.Visible)
            {
                mWeighingBill.moneyType = this.MoneyTypeCb.SelectedIndex;
            }
            else
            {
                mWeighingBill.moneyType = 0;
            }

        }

        #endregion


    }
}
